﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.PinBan.PB.View;
using System.Threading;
using System.Windows.Forms;
using SmallERP.Bll.PinBan;

namespace SmallERP.PinBan.PB.Presenter
{
    public class FullAutoPBPresenter
    {
        private IFullAutoPBView _view;
        private FullAutoPBBLL _bll;
        int SusBoardNum = 0;//累计拼的大板数
        int subPhotoNum = 0;//累计拼的照片数
        private float _curWraste = 0.0f;

        Thread thAutoMakeup = null;

        public FullAutoPBPresenter(IFullAutoPBView view)
        {
            _view = view;
            _bll = new FullAutoPBBLL(DataTransfer.Addr, DataTransfer.Port);
            _bll.AutoPBResultEvent += new GetFAutoPBResult(_bll_AutoPBResultEvent);
        }

        /// <summary>
        /// 显示拼板的结果信息
        /// </summary>
        /// <param name="result"></param>
        /// <param name="wrate"></param>
        /// <param name="Pnum"></param>
        /// <param name="overPnum"></param>
        /// <param name="msg"></param>
        void _bll_AutoPBResultEvent(int result, float wrate, int Pnum, int overPnum, string msg)
        {
            string strResult = "失败";
            string strStatus = "...";
            string strCurBStatus = "";

            switch (result)
            {
                case 0: SusBoardNum++;
                    subPhotoNum += Pnum;
                    //TotalPNum += overPnum;
                    strStatus = "成功拼板";
                    //strResult = "已拼:  " + SusBoardNum.ToString() + " 块大板    当前拼的浪费率：" + (wrate * 100).ToString("0.00") + "%    已拼照片数： " + TotalPNum.ToString() + " 剩余照片数： " + Pnum.ToString();
                    //strResult = "已拼:  " + SusBoardNum.ToString() + " 块大板    当前拼的浪费率：" + (wrate * 100).ToString("0.00") + "%    已拼照片数： " + TotalPNum.ToString();
                    strCurBStatus = "当前拼的浪费率：" + (wrate * 100).ToString("0.00") + "%  本次照片数：" + Pnum.ToString();
                    _view.UpCtrlStatus(strCurBStatus);
                    break;
                case 1: strStatus = "图片已全部拼完";
                    _view.SetBtn("开始", true);
                    break;
                case 2: strStatus = "自动拼板程序无法拼剩余的图片，请手动拼板!";
                    _view.SetBtn("开始", true);
                    break;
                case 3: strStatus = "在向数据库添加数据时出错，自动拼板将停止!";
                    _view.SetBtn("开始", true);
                    break;
                case 4: strStatus = "正在检查拼板结果...";
                    break;
                case 5:
                    strStatus = "正在收集数据...";
                    break;
                case 6:
                    strStatus = "正在进行自动拼板...";
                    break;
                case 7:
                    strStatus = "正在检查拼板结果,   当前浪费率：" + (wrate * 100).ToString("0.00") + "%";
                    break;
                case 8://显示清柜的照片数量
                    _view.UpCtrlClearBoxCount("还剩清柜照片：" + overPnum.ToString() + " 张");
                    break;
                case 9://显示剩余照片总数 
                    _view.UpCtrlRemainPs("还剩未拼照片：" + overPnum.ToString() + " 张");
                    break;
                case 10://停止全自动拼板
                    strStatus = "停止";
                    _view.SetBtn("开始", true);
                    break;
                default:
                    break;
            }
            strResult = "已拼:  " + SusBoardNum.ToString() + " 块大板    已拼照片数： " + subPhotoNum.ToString();
            _view.UpCtrl(strResult, strStatus);
        }

        internal void StartFAutoPB()
        {
            _curWraste = _view.CtmRaste/100;
            SusBoardNum = 0;
            try
            {
                if (thAutoMakeup == null)
                {
                    thAutoMakeup = new Thread(FAutoPB);
                    thAutoMakeup.Start();
                }
                else if (thAutoMakeup.ThreadState == ThreadState.Stopped || thAutoMakeup.ThreadState == ThreadState.Background || thAutoMakeup.ThreadState == ThreadState.WaitSleepJoin || thAutoMakeup.ThreadState == ThreadState.Suspended)
                {
                    thAutoMakeup = new Thread(FAutoPB);
                    thAutoMakeup.Start();
                }
                else
                {
                    MessageBox.Show("无法执行操作，请重试！");
                    _view.CloseFrm();
                }

                _view.SetBtn("暂停", true);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void FAutoPB()
        {
            //int result = 0;
            _bll.IsStop = false;
            //result =
            _bll.FAUTOPB(_curWraste, DataTransfer.curTemplateInfo, DataTransfer.CurPrinerInfo);

            //if (result == 3)//需要正常结束
            //{
            //    StopFAutoPB();
            //}
        }

        internal void StopFAutoPB()
        {
            try
            {
                _bll.IsStop = true;
                _view.SetBtn("正在停止拼板", false);
            }
            catch (ThreadAbortException ex)
            {
                Thread.ResetAbort();
            }
            finally
            {

            }
        }
    }
}
